﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using MsForgeReboot.Framework.Entities;
using System.Net;
using MsForgeReboot.Framework.Interfaces;

namespace MsForgeReboot.Framework.TownCrierChannels {
    /// <summary>
    /// Represents a base class for all channels that are 
    /// using REST to publish the events.
    /// </summary>
    public class RestTransport {

        // do we put some fields here?
        protected  IRestClient _client;
        protected IRestRequest _request;


        //public RestTransport() : this(new RestClient(), new RestRequest()) { }

        public RestTransport(IRestClient client, IRestRequest request) {
            _client = client;
            _request = request;
        }

        //public abstract DeliveryResult ScheduleDelivery(Event source);

        //public abstract DeliveryResult ScheduleDelivery();

        /// <summary>
        /// Performs a POST request using the authenticator and the parameters on the URL
        /// specified.
        /// </summary>
        /// <param name="baseUrl">Base URL for the REST client</param>
        /// <param name="postResource">URL segment for REST request</param>
        /// <param name="authenticator">IAuthenticator for this request</param>
        /// <param name="parameters">Any parameters that are needed for the request</param>
        /// <returns>RestResponse object that is the response from the request</returns>
        public DeliveryResult Post(string baseUrl, string postResource, IAuthenticator authenticator, Parameter[] parameters) {
            return Post(baseUrl, postResource, authenticator, parameters, DataFormat.Json);
        }

        public DeliveryResult Post(string baseUrl, string postResource, IAuthenticator authenticator, Parameter[] parameters, DataFormat format){
            return PerformRequest(baseUrl, postResource, authenticator, parameters, format, Method.POST);
            
        }

        public DeliveryResult Get(string baseUrl, string postResource, IAuthenticator auth, Parameter[] parameters) {
            throw new NotImplementedException();
            //return Get(baseUrl, postResource, auth, parameters, null);
        }

        public DeliveryResult Get(string baseUrl, string getResource, IAuthenticator auth, Parameter[] parameters, DataFormat dataFormat) {
            //throw new NotImplementedException();
            return PerformRequest(baseUrl,getResource,auth,parameters, dataFormat, Method.GET);

        }

        private DeliveryResult PerformRequest(string baseUrl, string restResource, IAuthenticator auth, Parameter[] parameters, DataFormat format, Method method){
            //throw new NotImplementedException();
            _client.BaseUrl = baseUrl;
            _client.Authenticator = auth;
            _request.Resource = restResource;
            _request.Method = method;
            _request.RequestFormat = format;

            if (parameters != null && parameters.Length > 0) {
                foreach (Parameter param in parameters) {
                    _request.AddParameter(param);
                }
            }

            //IRestResponse response = _client.Execute((IRestRequest)_request);
            IRestResponse response = this._client.Execute(this._request);

            return new DeliveryResult {
                DeliveryChannel = "",
                Message = response.Content,
                Success = (response.StatusCode == HttpStatusCode.OK && response.ResponseStatus == ResponseStatus.Completed) ? true : false,
                Status = DeliveryStatus.Completed,
                DeliveredOn = DateTime.UtcNow
            };

        }







    }
}
